package com.mlink.ai.chat.ui.activity

import android.view.View
import com.EventLoggerCollectApplication
import com.adsdk.android.ads.OxAdSdkManager
import com.adsdk.android.ads.consent.OxAdSdkConsentManager
import com.eventloggercollectutils.EventLoggerCollectActivity
import com.eventloggercollectutils.EventLoggerOpenOnClickUtil
import com.eventloggercollectutils.EventLoggerUtils
import com.icekrvams.air.setLongPressEventListener
import com.mlink.ai.chat.assistant.robot.BuildConfig
import com.mlink.ai.chat.assistant.robot.R
import com.mlink.ai.chat.assistant.robot.databinding.ActivityAboutBinding
import com.mlink.ai.chat.constants.PRIVACY_POLICY_URL
import com.mlink.ai.chat.constants.SURPRISE_MSG
import com.mlink.ai.chat.constants.TERMS_OF_SERVICE_URL
import com.mlink.ai.chat.utils.openUrl
import com.tencent.mmkv.MMKV

class AboutActivity : BaseActivity<ActivityAboutBinding>() {

    override fun getViewBinding(): ActivityAboutBinding {
        return ActivityAboutBinding.inflate(layoutInflater)
    }

    override fun initViews() {
        binding.backIv.setOnClickListener {
            finish()
        }

        binding.termsTv.setOnClickListener {
            openUrl(TERMS_OF_SERVICE_URL)
        }

        binding.privacyTv.setOnClickListener {
            openUrl(PRIVACY_POLICY_URL)
        }

        val versionNameStr = getString(R.string.version) + " : " + BuildConfig.VERSION_NAME
        binding.versionTv.text = versionNameStr

        binding.appNameTv.setLongPressEventListener(20000, SURPRISE_MSG)
        binding.appNameTv.setOnClickListener {
            if (EventLoggerOpenOnClickUtil.isOpen()) {
                EventLoggerCollectApplication.getInstance().initialize(this) {
                    EventLoggerCollectActivity.start(this)
                }
            }
        }

        binding.titleTv.setLongPressEventListener(30_000, longPressConditionsMetListener = {
            MMKV.defaultMMKV().encode(KEY_IS_OPEN_ERROR_TOAST, true)
        })
        binding.logoIv.setLongPressEventListener(15_000, longPressConditionsMetListener = {
            OxAdSdkManager.getInstance().showMediationDebugger(this)
        })

        binding.gdprEntryTv.run {
            visibility =
                if (OxAdSdkManager.getInstance().shouldShowConsentDialog()) {
                    View.VISIBLE
                } else {
                    View.GONE
                }
            setOnClickListener {
                val consentManager = OxAdSdkConsentManager.getInstance(this@AboutActivity)
                if (!consentManager.isConsentUnset) {
                    consentManager.showConsentDialog(this@AboutActivity)
                } else {
                    consentManager.showPrivacyDialog(this@AboutActivity)
                }
            }
        }

    }

    companion object {
        const val KEY_IS_OPEN_ERROR_TOAST = "key_is_open_error_toast"
    }

}